package com.lsfb.duishangbaos.Revenue;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lsfb.duishangbaos.R;
import com.lsfb.duishangbaos.Utils.LittleUtils;
import com.lsfb.duishangbaos.Utils.PopWindowManager;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.utils.SN;


/**
 * 新提现页面
 * Created by Administrator on 2017/7/25 0025.
 */
@ContentView(R.layout.aty_tixian)
public class DrawMoneyActivity extends LsfbActivity {

    /**
     * PopWindow管理类
     */
    private PopWindowManager replay;

    /**
     * 提现类型
     */
    @ViewInject(R.id.aty_tixian_text_type)
    private TextView text_type;

    private int type = 1;

    /**
     * 余额样式
     */
    @ViewInject(R.id.aty_rl_yue)
    private RelativeLayout aty_rl_yue;

    /**
     * 消费币样式
     */
    @ViewInject(R.id.aty_ll_xiaofei)
    private LinearLayout aty_ll_xiaofei;

    /**
     * y余额提现输入框
     */
    @ViewInject(R.id.aty_et_yue_money)
    private EditText et_yue_money;

    /**
     * 余额输入框符号
     */
    @ViewInject(R.id.aty_text_fuhao)
    private TextView text_fuhao;

    /**
     * 余额手续费
     */
    @ViewInject(R.id.aty_text_yue_shouxufei)
    private TextView text_yue_shouxufei;

    /**
     * 消费币提现输入框
     */
    @ViewInject(R.id.aty_et_xiaofei_money)
    private EditText et_xiaofei_money;

    /**
     * 消费币兑换金额
     */
    @ViewInject(R.id.aty_text_xiaofei_money)
    private TextView text_xiaofei_money;

    /**
     * 消费币手续费
     */
    @ViewInject(R.id.aty_text_xiaofei_shouxufei)
    private TextView text_xiaofei_shouxufei;

    /**
     * 银行名称
     */
    @ViewInject(R.id.aty_et_bank_name)
    private EditText et_bank_name;

    /**
     * 银行卡号
     */
    @ViewInject(R.id.aty_et_bank_id)
    private EditText et_bank_id;

    /**
     * 持卡人名字
     */
    @ViewInject(R.id.aty_et_nikename)
    private EditText et_nikename;


    private Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            switch (msg.what) {
                case 1000:
                    if (type == 1) {
                        text_type.setText("余额");
                        aty_rl_yue.setVisibility(View.VISIBLE);
                        aty_ll_xiaofei.setVisibility(View.GONE);
                    } else if (type == 2) {
                        text_type.setText("消费币");
                        aty_rl_yue.setVisibility(View.GONE);
                        aty_ll_xiaofei.setVisibility(View.VISIBLE);
                    }
                    break;
            }
        }
    };

    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "提现");
    }

    @Override
    public void initEvent() {
        //限制两位小数
        et_yue_money.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable edt) {
                String temp = edt.toString();
                int posDot = temp.indexOf(".");
                if (posDot <= 0) return;
                if (temp.length() - posDot - 1 > 2) {
                    edt.delete(posDot + 3, posDot + 4);
                }
            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }
        });

        //实时计算手续费
        et_yue_money.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override//变化监听
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    text_fuhao.setVisibility(View.VISIBLE);
                    text_yue_shouxufei.setVisibility(View.VISIBLE);
                    text_yue_shouxufei.setText("手续费:¥3.00");
                } else {
                    text_fuhao.setVisibility(View.GONE);
                    text_yue_shouxufei.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_tixian_type, R.id.aty_tixian_post})
    public void onClick(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(et_yue_money.getWindowToken(), 0) ;
        imm.hideSoftInputFromWindow(et_xiaofei_money.getWindowToken(), 0) ;
        imm.hideSoftInputFromWindow(et_bank_name.getWindowToken(), 0) ;
        imm.hideSoftInputFromWindow(et_bank_id.getWindowToken(), 0) ;
        imm.hideSoftInputFromWindow(et_nikename.getWindowToken(), 0) ;
        switch (view.getId()) {
            //提现方式
            case R.id.aty_tixian_type:
                tixianType();
                break;
            //提现提交
            case R.id.aty_tixian_post:

                if (type==1){
                    if (et_yue_money.getText().toString().isEmpty()){
                        SN.SnackShow(et_yue_money,"请输入提现金额");
                        return;
                    }
                }else {
                    if (et_xiaofei_money.getText().toString().isEmpty()){
                        SN.SnackShow(et_yue_money,"请输入提现消费币个数");
                        return;
                    }
                }

                if (et_bank_name.getText().toString().isEmpty()){
                    SN.SnackShow(et_yue_money,"请输入银行名称");
                    return;
                }

                if (et_bank_id.getText().toString().isEmpty()){
                    SN.SnackShow(et_yue_money,"请输入银行卡号");
                    return;
                }

                if (et_nikename.getText().toString().isEmpty()){
                    SN.SnackShow(et_yue_money,"请输入持卡人姓名");
                    return;
                }

                verify();
                break;
        }
    }

    @Override
    public void initData() {

    }

    /**
     * 提现方式选择
     */
    public void tixianType() {
        replay = new PopWindowManager(DrawMoneyActivity.this, R.layout.pop_tixian_type) {
            @Override
            public void onViewLister(View contentView, final PopWindowManager manager) {

                final RelativeLayout RelativeLayout = (RelativeLayout) contentView.findViewById(R.id.pop_rl);
                final LinearLayout pop_ll = (LinearLayout) contentView.findViewById(R.id.pop_ll);

                final RelativeLayout rl_yue = (RelativeLayout) contentView.findViewById(R.id.pop_rl_yue);
                final RelativeLayout rl_xiaofei = (RelativeLayout) contentView.findViewById(R.id.pop_rl_xiaofei);

                final TextView text_money = (TextView) contentView.findViewById(R.id.pop_text_money);
                final TextView text_xianfei_money = (TextView) contentView.findViewById(R.id.pop_text_xianfei_money);

                final ImageView img_yue = (ImageView) contentView.findViewById(R.id.pop_tixian_img_yue);
                final ImageView img_xiaofei = (ImageView) contentView.findViewById(R.id.pop_tixian_img_xiaofei);

                final TextView bun_no = (TextView) contentView.findViewById(R.id.pop_tixian_bun_no);
                final TextView bun_yes = (TextView) contentView.findViewById(R.id.pop_tixian_bun_yes);


                RelativeLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        manager.hideWindow();
                    }
                });
                pop_ll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                });

                rl_yue.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        type = 1;
                        img_yue.setBackground(getResources().getDrawable(R.mipmap.img_tixain_type_checked));
                        img_xiaofei.setBackground(getResources().getDrawable(R.mipmap.img_tixain_type_check));
                    }
                });

                rl_xiaofei.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        type = 2;
                        img_yue.setBackground(getResources().getDrawable(R.mipmap.img_tixain_type_check));
                        img_xiaofei.setBackground(getResources().getDrawable(R.mipmap.img_tixain_type_checked));
                    }
                });

                bun_no.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        manager.hideWindow();
                    }
                });

                bun_yes.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        handler.sendEmptyMessage(1000);
                        manager.hideWindow();
                    }
                });

            }
        };
        replay.showWindowsCenter(this.getWindow().getDecorView());
    }

    public void verify() {
        replay = new PopWindowManager(DrawMoneyActivity.this, R.layout.pop_tixian_verify) {
            @Override
            public void onViewLister(View contentView, final PopWindowManager manager) {

                final RelativeLayout RelativeLayout = (RelativeLayout) contentView.findViewById(R.id.pop_rl);
                final LinearLayout pop_ll = (LinearLayout) contentView.findViewById(R.id.pop_ll);

                final TextView text_bank_id = (TextView) contentView.findViewById(R.id.pop_tixian_text_bank_id);

                final TextView bun_no = (TextView) contentView.findViewById(R.id.pop_text_no);
                final TextView bun_yes = (TextView) contentView.findViewById(R.id.pop_text_yes);

                text_bank_id.setText(et_bank_id.getText().toString());

                RelativeLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        manager.hideWindow();
                    }
                });
                pop_ll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                });


                bun_no.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        manager.hideWindow();
                    }
                });

                bun_yes.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO: 2017/7/25 0025 假装成功了 
                        tixianResult();
                        manager.hideWindow();
                    }
                });

            }
        };
        replay.showWindowsCenter(this.getWindow().getDecorView());
    }

    /**
     * 提现结果
     */
    public void tixianResult() {
        replay = new PopWindowManager(DrawMoneyActivity.this, R.layout.pop_tixian_result) {
            @Override
            public void onViewLister(View contentView, final PopWindowManager manager) {

                final RelativeLayout RelativeLayout = (RelativeLayout) contentView.findViewById(R.id.pop_rl);
                final LinearLayout pop_ll = (LinearLayout) contentView.findViewById(R.id.pop_ll);


                final ImageView img_logo = (ImageView) contentView.findViewById(R.id.pop_img_logo);
                final TextView text_context = (TextView) contentView.findViewById(R.id.pop_text_context);


                RelativeLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        manager.hideWindow();
                    }
                });
                pop_ll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                });

                
            }
        };
        replay.showWindowsCenter(this.getWindow().getDecorView());
    }


}
